import sys

class output:
    LOGMODE = 1
    STDMODE = 2
    
    None_Request = 0
    GET_Request = 1
    PUT_Request = 2
    GET_PUT_Request = 3 

    def __init__(self, user="shafiq", writemode=2, mask = 3):
        self.stacklogFileName = "stacklog" + user +".txt"
        self.mode = writemode
        self.log = []
        self.logFile = file(self.stacklogFileName, "a")
        self.request_mask = mask
    
    def append(self, value, isError = False):
        if self.mode is output.LOGMODE:
            self.log.append(value)
        elif self.mode is output.STDMODE:
            if isError is False:
                sys.stdout.write(value)
            else:
                logfile = file("ErrorLog.txt", "a")
                logfile.write(value + "\n")
                logfile.close()
                
    def clear(self):
        self.log = []
        self.logFile = file(self.stacklogFileName, "a")
        
    def __repr__(self):
        value = ''
        if self.mode is output.LOGMODE:
            value += "Printing output in LOGMODE: \n"
            for out in self.log:
                value += out
        elif self.mode is output.STDMODE:
            value += "No print for STDMode"
            
        return value
    
    def logthis(self, text, request_bit = 3):
        if self.request_mask & request_bit is request_bit:
            if self.logFile is None:
                self.logFile = file(self.stacklogFileName, "a")
            self.logFile.write("\r" + text + " ")